<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2024/12/26
 * 描述:
 */

namespace app\service;

use app\model\order\Order;
use app\model\order\OrderCommission;
use app\model\order\OrderExpress;
use app\result\order\OrderResult;
use app\result\UserMsgResult;

class OrderService
{
    use BaseService;

    public static function pay_expire(): array
    {
        $where = [];
        $where[] = ['order_status', '=', 0];
        $where[] = ['pay_status', '=', 0];
        $data = (new Order())->data_list_nopage($where, 'order_id,order_sn,user_id,pay_status,pay_expire,parent_user_id');
        if (!$data) {
            return data_return_arr('没有需要检查支付的订单', 0);
        }
        $cancelData = [];
        $noticeData = [];
        foreach ($data as $v) {
            if ($v['pay_expire'] < time()) {
                $cancelData[] = [
                    'order_id' => $v['order_id'],
                    'order_sn' => $v['order_sn'],
                    'order_status' => 6,
                    'cancel_time' => date('Y-m-d H:i:s'),
                ];
                continue;
            }
            if ($v['pay_expire'] < time() + 15 * 60) {
                $noticeData[] = [
                    'order_id' => $v['order_id'],
                    'order_sn' => $v['order_sn'],
                    'user_id' => $v['user_id'],
                    'title' => '订单支付即将超时',
                    'content' => '订单支付即将超时，请及时支付',
                ];
            }
        }
        if ($cancelData) {
            foreach ($cancelData as $v) {
                $res = (new Order())->data_update(['order_id' => $v['order_id']], $v);
                if ($res) {
                    (new OrderCommission())->data_update(['order_id' => $v['order_id']], ['status' => -1]);//佣金取消
                    OrderResult::add_order_log([
                        'order_id' => $v['order_id'],
                        'order_sn' => $v['order_sn'],
                        'remark' => '订单超时未支付已取消',
                    ]);

                }
            }
        }
        if ($noticeData) {
            foreach ($noticeData as $v) {
                (new UserMsgResult())->add($v['user_id'], [
                    'title' => $v['title'],
                    'content' => $v['content'],
                    'type' => 1,
                    'path_info' => [
                        'title' => '订单详情',
                        'path' => '/pages/packageA/order/detail?order_sn=' . $v['order_sn'],
                        'jump_type' => 'inner',//inner=内页，tab=底部导航，link=外链
                    ]
                ]);
            }
        }
        return data_return_arr('执行完成', 0);
    }

    public static function do_refund($order, $applyLog): array
    {
        //1=微信，2=支付宝，3=转账，4=余额
        $pay_type = $order['pay_type'];
        $refund_price = $applyLog['refund_price'] > 0 ? $applyLog['refund_price'] : $order['pay_price'];
        if ($pay_type == 2) {
            $res = (new PaymentService())->refund_alipay([
                'order_sn' => $order['order_sn'],
                'refund_sn' => $order['order_sn'] . '_' . date('YmdHis'),
                'trade_no' => $order['trans_no'],
                'refund_amount' => $refund_price,
                'reason' => $applyLog['reason'] ?? "不想要了",
            ]);
            if ($res['code'] == 0) {
                return self::success_return('退款成功');
            } else {
                return self::error_return($res['msg']);
            }
        } elseif ($pay_type == 4) {
            $res = (new CreditService())::credit_action($order['user_id'], $refund_price, [
                'order_sn' => $order['order_sn'],
                'remark' => $order['order_sn'] . '订单退款',
            ]);
            if ($res) {
                return self::success_return('退款成功');
            } else {
                return self::error_return('退款失败');
            }
        }

        return self::error_return('暂不支持该支付方式原路退款');
    }
}
